package com.haoji.upload.common.Value.mvc;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

/**
 * @program: upload
 * @description:
 * @author: xiaolei
 * @create: 2021-04-15 17:52
 **/
public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        super();
        // 设置null转换""
        getSerializerProvider().setNullValueSerializer(new NullSerializer());
        // 设置日期转换yyyy-MM-dd HH:mm:ss
        setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    }

    // null的JSON序列
    private class NullSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
                throws IOException, JsonProcessingException {
            jgen.writeString("");
        }
    }

}
